﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using BangleCMS.DBUtility;

namespace BangleCMS.DAL
{
    //t_webnav
    public partial class t_webnavDAL
    {

        public bool Exists(int WebNavId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_webnav");
            strSql.Append(" where ");
            strSql.Append(" WebNavId = @WebNavId  ");
            MySqlParameter[] parameters = {
					new MySqlParameter("@WebNavId", MySqlDbType.Int32)
			};
            parameters[0].Value = WebNavId;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BangleCMS.Model.t_webnav model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_webnav(");
            strSql.Append("ParentId,Title,Code,IsSys,Href,OrderNo,CreateTime,CreateUserId,UpdateTime,UpdateUserId,Valid");
            strSql.Append(") values (");
            strSql.Append("@ParentId,@Title,@Code,@IsSys,@Href,@OrderNo,@CreateTime,@CreateUserId,@UpdateTime,@UpdateUserId,@Valid");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            MySqlParameter[] parameters = {
			            new MySqlParameter("@ParentId", MySqlDbType.Int32,11) ,            
                        new MySqlParameter("@Title", MySqlDbType.VarChar,200) ,            
                        new MySqlParameter("@Code", MySqlDbType.VarChar,100) ,            
                        new MySqlParameter("@IsSys", MySqlDbType.Int32,11) ,            
                        new MySqlParameter("@Href", MySqlDbType.VarChar,500) ,            
                        new MySqlParameter("@OrderNo", MySqlDbType.VarChar,20) ,            
                        new MySqlParameter("@CreateTime", MySqlDbType.VarChar,20) ,            
                        new MySqlParameter("@CreateUserId", MySqlDbType.Int32,11) ,            
                        new MySqlParameter("@UpdateTime", MySqlDbType.VarChar,20) ,            
                        new MySqlParameter("@UpdateUserId", MySqlDbType.Int32,11) ,            
                        new MySqlParameter("@Valid", MySqlDbType.Int32,11)             
              
            };

            parameters[0].Value = model.ParentId;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.Code;
            parameters[3].Value = model.IsSys;
            parameters[4].Value = model.Href;
            parameters[5].Value = model.OrderNo;
            parameters[6].Value = model.CreateTime;
            parameters[7].Value = model.CreateUserId;
            parameters[8].Value = model.UpdateTime;
            parameters[9].Value = model.UpdateUserId;
            parameters[10].Value = model.Valid;

            object obj = DbHelperMySQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BangleCMS.Model.t_webnav model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_webnav set ");

            strSql.Append(" ParentId = @ParentId , ");
            strSql.Append(" Title = @Title , ");
            strSql.Append(" Code = @Code , ");
            strSql.Append(" IsSys = @IsSys , ");
            strSql.Append(" Href = @Href , ");
            strSql.Append(" OrderNo = @OrderNo , ");
            strSql.Append(" CreateTime = @CreateTime , ");
            strSql.Append(" CreateUserId = @CreateUserId , ");
            strSql.Append(" UpdateTime = @UpdateTime , ");
            strSql.Append(" UpdateUserId = @UpdateUserId , ");
            strSql.Append(" Valid = @Valid  ");
            strSql.Append(" where WebNavId=@WebNavId ");

            MySqlParameter[] parameters = {
			             new MySqlParameter("@WebNavId", MySqlDbType.Int32,11) ,                         new MySqlParameter("@ParentId", MySqlDbType.Int32,11) ,                         new MySqlParameter("@Title", MySqlDbType.VarChar,200) ,                         new MySqlParameter("@Code", MySqlDbType.VarChar,100) ,                         new MySqlParameter("@IsSys", MySqlDbType.Int32,11) ,                         new MySqlParameter("@Href", MySqlDbType.VarChar,500) ,                         new MySqlParameter("@OrderNo", MySqlDbType.VarChar,20) ,                         new MySqlParameter("@CreateTime", MySqlDbType.VarChar,20) ,                         new MySqlParameter("@CreateUserId", MySqlDbType.Int32,11) ,                         new MySqlParameter("@UpdateTime", MySqlDbType.VarChar,20) ,                         new MySqlParameter("@UpdateUserId", MySqlDbType.Int32,11) ,                         new MySqlParameter("@Valid", MySqlDbType.Int32,11)               
            };

            parameters[0].Value = model.WebNavId;
            parameters[1].Value = model.ParentId;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.Code;
            parameters[4].Value = model.IsSys;
            parameters[5].Value = model.Href;
            parameters[6].Value = model.OrderNo;
            parameters[7].Value = model.CreateTime;
            parameters[8].Value = model.CreateUserId;
            parameters[9].Value = model.UpdateTime;
            parameters[10].Value = model.UpdateUserId;
            parameters[11].Value = model.Valid;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int WebNavId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_webnav ");
            strSql.Append(" where WebNavId=@WebNavId");
            MySqlParameter[] parameters = {
					new MySqlParameter("@WebNavId", MySqlDbType.Int32)
			};
            parameters[0].Value = WebNavId;


            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string WebNavIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_webnav ");
            strSql.Append(" where ID in (" + WebNavIdlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BangleCMS.Model.t_webnav GetModel(int WebNavId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select WebNavId, ParentId, Title, Code, IsSys, Href, OrderNo, CreateTime, CreateUserId, UpdateTime, UpdateUserId, Valid  ");
            strSql.Append("  from t_webnav ");
            strSql.Append(" where WebNavId=@WebNavId");
            MySqlParameter[] parameters = {
					new MySqlParameter("@WebNavId", MySqlDbType.Int32)
			};
            parameters[0].Value = WebNavId;


            BangleCMS.Model.t_webnav model = new BangleCMS.Model.t_webnav();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["WebNavId"].ToString() != "")
                {
                    model.WebNavId = int.Parse(ds.Tables[0].Rows[0]["WebNavId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentId"].ToString() != "")
                {
                    model.ParentId = int.Parse(ds.Tables[0].Rows[0]["ParentId"].ToString());
                }
                model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                model.Code = ds.Tables[0].Rows[0]["Code"].ToString();
                if (ds.Tables[0].Rows[0]["IsSys"].ToString() != "")
                {
                    model.IsSys = int.Parse(ds.Tables[0].Rows[0]["IsSys"].ToString());
                }
                model.Href = ds.Tables[0].Rows[0]["Href"].ToString();
                model.OrderNo = ds.Tables[0].Rows[0]["OrderNo"].ToString();
                model.CreateTime = ds.Tables[0].Rows[0]["CreateTime"].ToString();
                if (ds.Tables[0].Rows[0]["CreateUserId"].ToString() != "")
                {
                    model.CreateUserId = int.Parse(ds.Tables[0].Rows[0]["CreateUserId"].ToString());
                }
                model.UpdateTime = ds.Tables[0].Rows[0]["UpdateTime"].ToString();
                if (ds.Tables[0].Rows[0]["UpdateUserId"].ToString() != "")
                {
                    model.UpdateUserId = int.Parse(ds.Tables[0].Rows[0]["UpdateUserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Valid"].ToString() != "")
                {
                    model.Valid = int.Parse(ds.Tables[0].Rows[0]["Valid"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM t_webnav ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM t_webnav ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperMySQL.Query(strSql.ToString());
        }


    }
}

